package com.example.lzy.viewpagerandfragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity implements View.OnClickListener {

	private ViewPager mViewPager;
	private FragmentPagerAdapter mAdapter;
	private List<Fragment> mFragments = new ArrayList<Fragment>();

	private MainTab01 tab01;
	private MainTab02 tab02;
	private MainTab03 tab03;
	private MainTab04 tab04;
//    @BindView(R.id.btn_tab_bottom_weixin)
//	ImageButton imageButton_weixin;
//	@BindView(R.id.btn_tab_bottom_friend)
//	ImageButton imageButton_friend;
//	@BindView(R.id.btn_tab_bottom_contact)
//	ImageButton imageButton_contact;
//	@BindView(R.id.btn_tab_bottom_setting)
//	ImageButton imageButton_setting;

	/**
	 * �ײ��ĸ���ť
	 */
	private LinearLayout mTabBtnWeixin;
	private LinearLayout mTabBtnFrd;
	private LinearLayout mTabBtnAddress;
	private LinearLayout mTabBtnSettings;


	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mViewPager = (ViewPager) findViewById(R.id.viewpagers);

		
		initView();
		

		findViewById(R.id.btn_tab_bottom_weixin).setOnClickListener(this);
		findViewById(R.id.btn_tab_bottom_friend).setOnClickListener(this);
		findViewById(R.id.btn_tab_bottom_contact).setOnClickListener(this);
		findViewById(R.id.btn_tab_bottom_setting).setOnClickListener(this);

		mAdapter = new FragmentPagerAdapter(getSupportFragmentManager())
		{

			@Override
			public int getCount()
			{
				return mFragments.size();
			}

			@Override
			public Fragment getItem(int arg0)
			{
				return mFragments.get(arg0);
			}
		};
		
		mViewPager.setAdapter(mAdapter);
		
		
		mViewPager.setOnPageChangeListener(new OnPageChangeListener()
		{

			private int currentIndex;

			@Override
			public void onPageSelected(int position)
			{
				resetTabBtn();
				switch (position)
				{
				case 0:
					((ImageButton) mTabBtnWeixin.findViewById(R.id.btn_tab_bottom_weixin))
							.setImageResource(R.drawable.tab_weixin_pressed);


					break;
				case 1:
					((ImageButton) mTabBtnFrd.findViewById(R.id.btn_tab_bottom_friend))
							.setImageResource(R.drawable.tab_find_frd_pressed);

					break;
				case 2:
					((ImageButton) mTabBtnAddress.findViewById(R.id.btn_tab_bottom_contact))
							.setImageResource(R.drawable.tab_address_pressed);

					break;
				case 3:
					((ImageButton) mTabBtnSettings.findViewById(R.id.btn_tab_bottom_setting))
							.setImageResource(R.drawable.tab_settings_pressed);

					break;
				}

				currentIndex = position;
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2)
			{

			}

			@Override
			public void onPageScrollStateChanged(int arg0)
			{
			}
		});


	}
	
	protected void resetTabBtn()
	{
		((ImageButton) mTabBtnWeixin.findViewById(R.id.btn_tab_bottom_weixin))
				.setImageResource(R.drawable.tab_weixin_normal);
		((ImageButton) mTabBtnFrd.findViewById(R.id.btn_tab_bottom_friend))
				.setImageResource(R.drawable.tab_find_frd_normal);
		((ImageButton) mTabBtnAddress.findViewById(R.id.btn_tab_bottom_contact))
				.setImageResource(R.drawable.tab_address_normal);
		((ImageButton) mTabBtnSettings.findViewById(R.id.btn_tab_bottom_setting))
				.setImageResource(R.drawable.tab_settings_normal);
	}

	private void initView()
	{

		mTabBtnWeixin = (LinearLayout) findViewById(R.id.id_tab_bottom_weixin);
		mTabBtnFrd = (LinearLayout) findViewById(R.id.id_tab_bottom_friend);
		mTabBtnAddress = (LinearLayout) findViewById(R.id.id_tab_bottom_contact);
		mTabBtnSettings = (LinearLayout) findViewById(R.id.id_tab_bottom_setting);

		 tab01 = new MainTab01();
		 tab02 = new MainTab02();
		 tab03 = new MainTab03();
		 tab04 = new MainTab04();
		mFragments.add(tab01);
		mFragments.add(tab02);
		mFragments.add(tab03);
		mFragments.add(tab04);
	}

	@Override
	public void onClick(View view) {
		resetTabBtn();
		int positions=0;

		switch (view.getId())
		{
			case R.id.btn_tab_bottom_weixin:
				((ImageButton) mTabBtnWeixin.findViewById(R.id.btn_tab_bottom_weixin))
						.setImageResource(R.drawable.tab_weixin_pressed);
				positions = 0;

				break;
			case R.id.btn_tab_bottom_friend:
				((ImageButton) mTabBtnFrd.findViewById(R.id.btn_tab_bottom_friend))
						.setImageResource(R.drawable.tab_find_frd_pressed);
				positions = 1;

				break;
			case R.id.btn_tab_bottom_contact:
				((ImageButton) mTabBtnAddress.findViewById(R.id.btn_tab_bottom_contact))
						.setImageResource(R.drawable.tab_address_pressed);
				positions = 2;

				break;
			case R.id.btn_tab_bottom_setting:
				((ImageButton) mTabBtnSettings.findViewById(R.id.btn_tab_bottom_setting))
						.setImageResource(R.drawable.tab_settings_pressed);
				positions = 3;

				break;
		}

		mViewPager.setCurrentItem(positions,false);
//		currentIndex = position;
	}
}
